# Copyright 2010 Giacomo Alzetta
# This file is part of "LimberEditor" and is distribuhited under the
# terms of the MIT license.

import unittest
from limber import viewmanager
from mocks import *


def testInstantiate():
    try:
        parent = MockApplication()
        displayer = MockDisplayer(parent)
        view_manager = viewmanager.ViewManager(parent, displayer)
    except Exception,e:
        assert False, 'Error:%r' % e


class TestViewManager(unittest.TestCase):

    def setUp(self):
        self.parent = MockApplication()
        self.displayer = MockDisplayer(self.parent)
        self.view_manager = viewmanager.ViewManager(self.parent, self.displayer)

    def testAddView(self):
        view = MockView(self.view_manager, None)
        try:
            self.view_manager.add_view(view)
        except Exception, e:
            self.fail('Could not add the view: %s' % e)

        self.assertEqual(1, self.view_manager.number_of_views(),
                         'The view was not added.')

        view2 = MockView(self.view_manager, None)
        try:
            self.view_manager.add_view(view2)
        except Exception, e:
            self.fail('Could not add the view: %s' % e)

        self.assertEqual(2, self.view_manager.number_of_views(),
                         'The view was not added.')

        try:
            self.view_manager.add_view(view2)
            self.fail('Should not be able to add the same view more times.')
        except viewmanager.ViewManagerError:
            pass
        except Exception, e:
            self.fail('Error: %s' % e)

    def testAddViewAddsItToTheDispalyer(self):

        view = MockView(self.view_manager, None)
        self.view_manager.add_view(view)

        self.assertTrue(self.displayer.has_view(view))

    def testRemoveView(self):
        view = MockView(self.view_manager, None)
        self.view_manager.add_view(view)

        try:
            self.view_manager.remove_view(view)
        except Exception, e:
            sef.fail('Could not remove the view: %s' % e)

        self.assertEqual(0, self.view_manager.number_of_views(),
                         'The view was not removed.')

        try:
            self.view_manager.remove_view(view)
            self.fail('Should not be able to remove a view more times.')
        except viewmanager.ViewManagerError:
            pass
        except Exception, e:
            self.fail('Error: %s' % e)

    def testRemoveViewClosesTheView(self):
        view = MockView(self.view_manager, None)
        self.view_manager.add_view(view)

        self.view_manager.remove_view(view)

        self.assertFalse(self.displayer.has_view(view))

    def testActiveWindow(self):
        view = MockView(self.parent, None)
        self.view_manager.add_view(view)

        view2 = MockView(self.parent, None)
        self.view_manager.add_view(view2)
        self.assertTrue(self.view_manager.active_view() is view,
                        'Adding a view changed the focus.')
